// pages/movement/movement.js
const app = getApp()
var router = require("../../router.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    nozan: true,
    nozanjson: [],
    list:[    
    ],
    page: 0,
  },
  //点赞
  toCollectother: function (event) {
    var index = event.target.dataset.ctr;   
    var list = this.data.list;
    var praise_type = list[index].praise_type;
    var id = list[index].id;
    var uid = list[index].uid;
    if (uid == wx.getStorageSync('uid') || praise_type == true){      
      return false;
    }
    var that = this;
    app.request({
      url: router.user.praiseStep,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        id: id,
      },
      success: function (data) {       
        if (data) {        
          list[index].praise_type = !praise_type;                   
          list[index].praise = list[index].praise + 1;        
          that.setData({
            list: list
          })
          // 交互反馈
          wx.showToast({
            title: '点赞',
            icon: 'success',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: '操作失败请重试',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (option) {
    this.setData({     
      page: 0,
      list: []
    });   
     this.getInfo();
  },

  onReachBottom: function () {
    this.getInfo();
  },

  getInfo: function(){
    var that = this;
    app.request({
      url: router.user.getStepList,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        page: that.data.page,
      },
      success: function (data) {        
        if (data.list.length > 0) {
          for (var i = 0; i < data.list.length; i++) {        
            var obj = {};  
            obj = data.list[i];
            that.data.list.push(obj);
          }         
          if (that.data.page == 0){
            that.setData({
              user: data.user,
            });            
          }        
          that.setData({
            list: that.data.list,
            page: that.data.page + 1
          });
        } else {
          wx.showToast({
            title: '没有更多了',
            icon: 'none'
          })
        }
      }
    })
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/movement/movement',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
})